﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using KinonekoSoftware.ArkSpark.Core;
using KinonekoSoftware.Extensions.Ioc;
using KinonekoSoftware.Extensions.Strings.SearchModels;

namespace KinonekoSoftware.ArkSpark.Services.Highlights
{
    public class DefaultHighlightVisualStyle : IHighlightVisualStyle
    {
        public static readonly DefaultHighlightVisualStyle Instance   = new DefaultHighlightVisualStyle();
        public static readonly SolidColorBrush             _Highlight = new SolidColorBrush(Colors.Orange);

        public void reStyle(TextBlock visual, IList<IHighlightToken> results, string text)
        {
            visual.Inlines.Clear();


            foreach (var result in results)
            {

                if (result.Segment.IsMatch)
                {
                    var v = result.CreateString(text);
                    var run = new Hyperlink(new Run(v))
                    {
                        Foreground = _Highlight,
                        FontSize   = 14,
                        Tag = result,
                    };

                    run.Click += OnHighlightElementClicked;
                    visual.Inlines.Add(run);
                }
                else
                {

                    var run = new Run(result.CreateString(text))
                    {
                        FontSize = 14,
                    };
                    visual.Inlines.Add(run);
                }

                //
                //
            }
        }

        private static void OnHighlightElementClicked(object sender, RoutedEventArgs e)
        {
            if (sender is Hyperlink hyperlink && hyperlink.Tag is IHighlightToken glossary)
            {
                Ioc.Get<IHighlightVisualNavigator>().Open(glossary);
            }
        }
    }
}